Today, we’re making interactive plots in plotly. We’ll make examples using the Instacart data from the p8105.datasets package.
library(tidyverse)
library(p8105.datasets)
library(dplyr)
library(flexdashboard)
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
data("Instacart")
## Warning in data("Instacart"): data set 'Instacart' not found
janitor::clean_names(instacart)
I decided to keep three variables: order_hour_of_day,
department, days_since_prior_order.
instacart_ =
instacart |>
select(order_hour_of_day, department, days_since_prior_order)
Use plotly to make a histogram plot, a box plot, and a bar graph. First, we are making a histogram to see the distribution of orders by hour. The most orders were placed at 2pm.
instacart_ |>
plot_ly(
x = ~order_hour_of_day, type = "histogram",
marker = list(color = "pink",line = list(color = "grey",width = 2))) |>
layout(title = "Hours At Which Orders Were Placed",
xaxis = list(title = "Order Hours"),
yaxis = list(title = "Frequency"))
Next up - We are making a box plot. We are looking at department by the number of days since prior order.
instacart |>
plot_ly(
x = ~department, y = ~days_since_prior_order, color = ~department,
type = "box")